package com.itheima.sys.controller;

import com.itheima.common.exception.NcException;
import com.itheima.common.exception.enums.ResponseEnum;
import com.itheima.common.util.BeanHelper;
import com.itheima.common.util.JsonUtils;
import com.itheima.common.vo.Result;
import com.itheima.common.vo.UserInfo;
import com.itheima.sys.entity.SysUser;
import com.itheima.sys.entity.UserDomain;
import com.itheima.sys.service.ISysUserService;
import com.itheima.sys.service.UserDominService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.List;


@RestController
public class HelloController {

    @GetMapping("/test")

    public Result test(){
        return new Result(true, 200, "操作成功", "hello");
    }

    @Autowired
    private UserDominService userDominService;


    @PostMapping("/user")
    public Result saveUser(@RequestBody UserDomain user){
        //如果年龄为空，则抛出异常，返回400状态码，返回错误提示消息
        if (user.getAge() == null) {
            throw new NcException(ResponseEnum.ERROR);
        }
        user = userDominService.saveUser(user);
        return Result.success("用户保存成功！", user);
    }


    @Autowired
    private ISysUserService sysUserService;


    /**
     * 查询所有用户
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ROLE_USER', 'user_query_all')")
    @GetMapping("/user/list")
    public Result<List<SysUser>> queryAllUser(){
        List<SysUser> list = sysUserService.list();
        return Result.success(list);
    }

    /**
     * 获取用户信息 方式一 在controller方法中通过参数获取当前认证用户信息
     * @return
     */
    @GetMapping("/getUserInfo1")
    public Result<Authentication> getUserInfo1(Authentication authentication){
        String principal = (String) authentication.getPrincipal();//获取到是认证信息 用户主体
        System.out.println(principal);
        UserInfo userInfo = JsonUtils.toBean(principal, UserInfo.class);
        return Result.success(authentication);
    }

    /**
     * 获取用户信息 方式二 在controller方法中通过参数获取当前认证用户信息
     * @return
     */
    @GetMapping("/getUserInfo2")
    public Result<Principal> getUserInfo2(Principal principal){
        System.out.println(principal);
        return Result.success(principal);
    }


    /**
     * 获取用户信息 方式三 通过security上下文对象获取用户信息
     * @return
     */
    @GetMapping("/getUserInfo3")
    public Result<UserInfo> getUserInfo3(Principal principal){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String userInfoJson = (String) authentication.getPrincipal();
        UserInfo userInfo = JsonUtils.toBean(userInfoJson, UserInfo.class);
        return Result.success(userInfo);
    }


}
